#!/usr/bin/env bash

NGINX_URL="$1"
NGINX_PORT="$2"
UMIRC_URL="$3"


SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
PROJECT_ROOT=$(dirname ${SCRIPT_DIR})

# 替换nginx配置文件
default_conf=${PROJECT_ROOT}/docker/nginx/conf.d/default.conf
# 获取本机(宿主机)ip
if [[ `uname` == 'Darwin' ]]; then
    echo "Mac OS"
    ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 172.17.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
elif [[ `uname` == 'Linux' ]]; then
    echo "Linux"
    ip=localhost
fi

if [ "$NGINX_URL" = "" ]; then
    NGINX_URL="${ip}:8080"
fi
echo "替换nginx反向代理地址"
# 替换反向代理地址
sed -ri "s/[^#]\ +proxy_pass\ *http:\/\/[^\s]+\/flow-platform\/;/proxy_pass http:\/\/${NGINX_URL}\/flow-platform\/;/g" ${default_conf}
if [ "$NGINX_PORT" = "" ]; then
    NGINX_PORT="listen 8000"
fi
sed -ri "s/listen 8080;/${NGINX_PORT};/g" ${default_conf}


if [ "$UMIRC_URL" = "" ]; then
    UMIRC_URL="http:\/\/localhost:8080\/"
fi
echo "替换.umirc.js转发地址"
umirc_js=${PROJECT_ROOT}/.umirc.js 
sed  -ri "s/ +target: *\"http:\/\/flowplatform-fe-daily.ingress.dayu.work\/\",/target: \"${UMIRC_URL}\",/g" ${umirc_js}
